﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Controls;
using Flexible.Implementions.Bases;
using Flexible.Interfaces;
using System.Windows;

namespace Flexible.Wpf
{
    public class FolderViewPlugin : PluginBase
    {
        FolderView tree;

        public override void Enable(IPluginActivateContext context)
        {
            tree = new FolderView();
            WindowPlugin win = (WindowPlugin)context.GetRequirement("Flexible.Wpf.Window");
            win.MainRegion.Children.Add(tree);
            tree.RefreshTree();
        }

        public FolderView FolderTree
        {
            get { return tree; }
        }

        public override void Disable(IPluginActivateContext context)
        {
            WindowPlugin win = (WindowPlugin)context.GetRequirement("Flexible.Wpf.Window");
            win.MainRegion.Children.Remove(tree);
        }

        public override IEnumerable<IPluginQuery> Requirements
        {
            get
            {
                yield return new PluginQuery("Flexible.Wpf.Window", new Version(1, 0, 0, 0));
            }
        }

        #region Info
        public override string Name
        {
            get { return "Folder Tree View"; }
        }

        public override string Description
        {
            get { return "Displays the folders in My Computer."; }
        }

        public override string Author
        {
            get { return "deerchao"; }
        }

        public override DateTime ReleaseTime
        {
            get { return new DateTime(2008, 4, 9); }
        }

        public override string Id
        {
            get { return "Flexible.Wpf.FolderView"; }
        }

        public override Version Version
        {
            get { return new Version(1, 0, 0, 0); }
        }

        public override Version CompatibleVersion
        {
            get { return new Version(1, 0, 0, 0); }
        }
        #endregion

        public class FolderView : TreeView
        {
            public void RefreshTree()
            {
                BeginInit();
                Items.Clear();
                foreach (DriveInfo drive in DriveInfo.GetDrives())
                {
                    FolderViewItem item = new FolderViewItem(drive.RootDirectory);
                    Items.Add(item);
                    if (drive.IsReady)
                        item.Populate(true);
                }
                EndInit();
            }
        }

        class FolderViewItem : TreeViewItem
        {
            public FolderViewItem(DirectoryInfo di)
            {
                this.di = di;
                this.Header = di.Name;
            }

            DirectoryInfo di;

            public void Populate(bool forceRefresh)
            {
                if (Items.Count == 0 || forceRefresh)
                {
                    BeginInit();
                    Items.Clear();

                    try
                    {
                        foreach (DirectoryInfo sub in di.GetDirectories())
                            Items.Add(new FolderViewItem(sub));
                    }
                    catch (DirectoryNotFoundException)
                    {
                        TreeView tree = Parent as TreeView;
                        if (tree != null)
                        {
                            tree.Items.Remove(this);
                            return;
                        }
                        TreeViewItem item = Parent as TreeViewItem;
                        if (item != null)
                        {
                            item.Items.Remove(this);
                            return;
                        }
                    }
                    catch (UnauthorizedAccessException)
                    {
                    }

                    EndInit();
                }
            }

            protected override void OnExpanded(RoutedEventArgs e)
            {
                base.OnExpanded(e);

                foreach (FolderViewItem sub in Items)
                {
                    sub.Populate(false);
                }
            }
        }
    }
}
